'----------------------------------------------
' Name: Ali Karbassi
'       Matt Latzke
'       Chris Hanegraaf
'       Brian Culp
' Date: May 1, 2007
' Description: Program 8
'              Class Form1
'----------------------------------------------
Imports UWPCS3340
Imports System.Threading

Public Class Form1

   Private endProgThread As Thread
   Private workingThread As ReaderWriter
   Private Delegate Sub enableButtons()
   Private exitCancel As enableButtons

   ''' <summary>
   ''' Creates new thread and starts it.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub spinup()

      endProgThread = New Thread(AddressOf endProgThreadSub)
      endProgThread.Start()

   End Sub

   ''' <summary>
   ''' Exit button
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
      cmdNewReader.Enabled = False
      cmdNewWriter.Enabled = False
      spinup()
   End Sub

   ''' <summary>
   ''' Handles the exit.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub endProgThreadSub()

      FinishReadWrite()

      Dim userAnswer As MsgBoxResult = MsgBox("Do you really want to exit?", MsgBoxStyle.YesNo)

      If userAnswer = MsgBoxResult.Yes Then
         endManager()
         Application.Exit()
      Else
         Me.exitCancel = AddressOf enableTheButtons
         Me.Invoke(exitCancel)
      End If

   End Sub

   ''' <summary>
   ''' Enables the buttons.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub enableTheButtons()

      cmdNewReader.Enabled = True
      cmdNewWriter.Enabled = True

   End Sub

   ''' <summary>
   ''' New reader button
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdNewReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles cmdNewReader.Click

      workingThread = New Reader
      workingThread.mainForm = Me
      workingThread.DisplayMsg = AddressOf DisplayMsg
      workingThread.SpinUp()

   End Sub

   ''' <summary>
   ''' New writer button.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub cmdNewWriter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles cmdNewWriter.Click

      workingThread = New Writer
      workingThread.mainForm = Me
      workingThread.DisplayMsg = AddressOf DisplayMsg
      workingThread.SpinUp()

   End Sub

   ''' <summary>
   ''' Handles the display
   ''' </summary>
   ''' <param name="theID"></param>
   ''' <param name="theState"></param>
   ''' <param name="Total"></param>
   ''' <remarks></remarks>
   Private Sub DisplayMsg(ByVal theID As String, ByVal theState As State, ByVal Total As Short)

      Select Case theState
         Case State.Wait
            lstWaiting.Items.Add(theID)
         Case State.Start
            lstWaiting.Items.Remove(theID)
            lstWorking.Items.Add(theID)
            txtLog.Text &= theID & ": Start to work" & vbCrLf
            txtLogScroll()
            txtTotal.Text = databaseModule._total.ToString
         Case State.Finish
            lstWorking.Items.Remove(theID)
            txtLog.Text &= theID & ": Finished work: Total is " & Total & vbCrLf
            txtLogScroll()
            txtTotal.Text = databaseModule._total.ToString
      End Select

   End Sub

   ''' <summary>
   ''' Scrolls the log text box.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub txtLogScroll()

      txtLog.SelectionStart = txtLog.Text.Length
      txtLog.ScrollToCaret()

   End Sub

   ''' <summary>
   ''' Form load. Starts the database module.
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      databaseModule.spinUp()

   End Sub

End Class
